/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdesk;

import java.io.IOException;
//import java.io.PrintWriter; // printwriter crap, delete it
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * View the details and updates of a ticket.
 *
 * Parameters:
 * 1) ticketid - obtained via URL
 *
 * Attributes for JSP Page:
 * 1) error - indicates errors
 * 2) helpTicket - the help ticket. May be null of ticket does not exist or
 *    if ticket is unspecified.
 * 3) helpTicketUpdates - ArrayList of updates to the help ticket
 *
 * Session variables:
 * 1) user
 *
 * @author brian
 */
public class TicketDetailServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        User user; // user that is viewing this page, part of session
        String ticketIdStr; // ticket user requested to see
        int ticketId; // integer version of it
        String error=""; // string used to allow the jsp page to display an error message
        HelpTicket ticket;
        ArrayList<HelpTicketUpdate> helpTicketUpdate;

        RequestDispatcher rd; // used for transferring control to a JSP page

        // remove this printwriter crap (response.setContentType, printwriter out)
       //response.setContentType("text/plain;charset=UTF-8");
       // PrintWriter out = response.getWriter();
        try {
            // the first thing we must do is ensure that the user is authenticated;
            // I've made a convenient function for it
            if ((user=HelpdeskUtil.authenticateUser(request, response)) != null)
            {
                // get parameter for which ticket we are viewing
                //
                // Should the string in quotes below be ticketId instead of
                // ticketid (capital 'I'), or does the case not matter here?
                // -Evan
                ticketIdStr = request.getParameter("ticketid");
                // parameters are always strings, so convert it
                try
                {
                    ticketId = Integer.parseInt(ticketIdStr);
                    ticket = HelpTicket.getById(ticketId);

                    // TODO: get updates here (Look at HelpTicketUpdate.getByTicketId)
                    helpTicketUpdate = HelpTicketUpdate.getByTicketId(ticketId);
                    // TODO: set attributes here. You should pass the objects/arrays,
                    // not their fields
                    request.setAttribute("helpTicket", ticket);
                    request.setAttribute("helpTicketUpdates", helpTicketUpdate);
                    // more hackish test stuff, you won't need this
                    /*out.println("Ticket information\n");
                    out.println("ID: " + ticket.getId());
                    out.println("User: " + ticket.getUser());
                    out.println("Area: " + ticket.getExpertise());
                    out.println("Title: " + ticket.getTitle());
                    out.println("Description: " + ticket.getDescription());*/
                }
                catch (NumberFormatException ex)
                {
                    error = "Invalid ticket id";

                }
                catch (SQLException ex)
                {
                    error = "SQL error " + ex.getMessage();
                }
                // this is where you display the JSP page, I have commented it out
                // for when it is ready
                request.setAttribute("error", error);
                rd = request.getRequestDispatcher("/WEB-INF/ticketdetails.jsp");
                rd.forward(request, response);
            }

            

            //out.println(error);
        } finally {
            // remove this printwriter crap
            //out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
